package com.lucaswangdev.logindemo.model.convert;

import com.alibaba.fastjson.JSON;
import com.lucaswangdev.logindemo.biz.request.user.UserCreateRequest;
import com.lucaswangdev.logindemo.biz.request.user.UserUpdateRequest;
import com.lucaswangdev.logindemo.model.domain.UserDO;
import com.lucaswangdev.logindemo.model.dto.UserDTO;
import com.lucaswangdev.logindemo.web.vo.UserVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mappings;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;
import java.util.Collections;
import java.util.List;

@Mapper(componentModel = "spring")
public interface UserConverter {
    UserConverter INSTANCE = Mappers.getMapper(UserConverter.class);

    @Mappings({
                })
    UserDTO convertRequest2DTO(UserCreateRequest request);

    @Mappings({
                })
    UserDTO convertRequest2DTO(UserUpdateRequest request);

    @Mappings({
    })
    UserDTO convertDO2DTO(UserDO userDO);

    @Mappings({
    })
    UserDO convertDTO2DO(UserDTO userDTO);

    List<UserVO> convertDTOList2VOList(List<UserDTO> tableData);

    @Mappings({
                })
    UserVO convertDTO2VO(UserDTO userDTO);

    @Named("listToJsonString")
    default String listToJsonString(List<String> list){
        return list == null ? null : JSON.toJSONString(list);
    }

    @Named("jsonStringToList")
    default List<String> jsonStringToList(String jsonString){
        if(jsonString == null) {
            return Collections.emptyList();
        }
        return JSON.parseArray(jsonString, String.class);
    }
}
